//
//  XQPublicIdentityPopAccureView.swift
//  Spark Peso
//
//  Created by 小琦 on 2025/2/7.
//

import UIKit

class XQPublicIdentityPopAccureView: UIView,UITableViewDelegate,UITableViewDataSource {
    
    var sureHandle: (() -> Void)?
    
    var cancelHandle: (() -> Void)?
    
    var clickBirthdayHandle : (() -> Void)?
    
    var textFieldChangedHandle: ((String,String) -> Void)?
    
    var model : XQOrderPublishIdentityInfoModel? {
        didSet {
            self.bottomLb.text = model?.connecticut
            self.tableView.snp.updateConstraints { make in
                make.height.equalTo((model?.penalty?.count ?? 0) * 84)
            }
            self.tableView.reloadData()
        }
    }
    
    private lazy var contentView : UIView = {
        let v = UIView()
        return v
    }()
    
    private lazy var bgImageView : UIImageView = {
        let imageView = UIImageView()
        imageView.image = UIImage(named: "pop_base_bg")
        imageView.isUserInteractionEnabled = true
        imageView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(imageClick)))
        return imageView
    }()
    
    private lazy var tableView : UITableView = {
        let tableView = UITableView()
        tableView.backgroundColor = .clear
        tableView.delegate = self
        tableView.dataSource = self
        tableView.separatorStyle = .none
        tableView.isScrollEnabled = false
        tableView.register(XQPublicIdentityAccurePopCell.self, forCellReuseIdentifier: "XQPublicIdentityAccurePopCell")
        return tableView
    }()
    
    private lazy var sureBtn : UIButton = {
        let btn = UIButton(type: .custom)
        btn.setTitle("OK", for: .normal)
        btn.setTitleColor(.white, for: .normal)
        btn.backgroundColor = .mainTextColor
        btn.layer.cornerRadius = 24
        btn.addTarget(self, action: #selector(sureClick), for: .touchUpInside)
        return btn
    }()
    
    private lazy var calcelBtn : UIButton = {
        let btn = UIButton(type: .custom)
        btn.setTitle("Close", for: .normal)
        btn.setTitleColor(.white, for: .normal)
        btn.layer.cornerRadius = 24
        btn.layer.borderWidth = 1
        btn.layer.borderColor = UIColor.white.cgColor
        btn.addTarget(self, action: #selector(cancelClick), for: .touchUpInside)
        return btn
    }()
    
    private lazy var topImageView : UIImageView = {
        let imageView = UIImageView()
        imageView.image = UIImage(named: "public_identity_top")
        return imageView
    }()
    
    private lazy var bottomLb : UILabel = {
        let lb = UILabel()
        lb.textColor = .xq_black
        lb.font = UIFont.systemFont(ofSize: 16,weight: .medium)
        lb.textAlignment = .center
        lb.numberOfLines = 0
        return lb
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.isUserInteractionEnabled = true
        makeUI()
        makeConstraints()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        makeUI()
        makeConstraints()
    }
    
    private func makeUI() {
        self.addSubview(self.contentView)
        self.contentView.addSubview(self.bgImageView)
        self.contentView.addSubview(self.sureBtn)
        self.contentView.addSubview(self.calcelBtn)
        self.bgImageView.addSubview(self.tableView)
        self.bgImageView.addSubview(self.bottomLb)
    }
    
    private func makeConstraints() {
        self.contentView.snp.makeConstraints { make in
            make.center.equalToSuperview()
        }
        self.bgImageView.snp.makeConstraints { make in
            make.top.equalTo(0)
            make.left.equalTo(0)
            make.right.equalTo(0)
            make.width.equalTo(UIDevice.xq_width() - 32)
            make.height.equalTo(420)
        }
        self.sureBtn.snp.makeConstraints { make in
            make.top.equalTo(self.bgImageView.snp.bottom).offset(30)
            make.left.equalTo(0)
            make.right.equalTo(0)
            make.height.equalTo(48)
        }
        self.calcelBtn.snp.makeConstraints { make in
            make.top.equalTo(self.sureBtn.snp.bottom).offset(15)
            make.left.equalTo(0)
            make.right.equalTo(0)
            make.height.equalTo(48)
            make.bottom.equalTo(0)
        }
        self.tableView.snp.makeConstraints { make in
            make.top.equalTo(30)
            make.left.right.equalTo(0)
            make.height.equalTo(0)
        }
        self.bottomLb.snp.makeConstraints { make in
            make.left.equalTo(15)
            make.right.equalTo(-15)
            make.top.equalTo(self.tableView.snp.bottom).offset(23)
        }
    }
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return model?.penalty?.count ?? 0
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "XQPublicIdentityAccurePopCell", for: indexPath) as! XQPublicIdentityAccurePopCell
        
        if let m = model {
            if (indexPath.row < (m.penalty?.count ?? 0)) {
                let penaltyModel = m.penalty![indexPath.row]
                cell.name = penaltyModel.situated
                cell.content = penaltyModel.prior
                cell.showArrow = penaltyModel.object == "statistically"
            }
        }
        
        cell.textFielHandle = {[weak self] (text) in
            guard let srongify = self else {return}
            srongify.textFieldChangedHandle?(srongify.model!.penalty![indexPath.row].object!,text)
        }
        cell.btnHandle = {[weak self] in
            guard let srongify = self else {return}
            srongify.clickBirthdayHandle?()
        }
        
        return cell
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 84
    }
    
    
    @objc private func imageClick() {
        self.endEditing(true)
    }
    
    @objc private func sureClick() {
        sureHandle?()
    }
    
    @objc private func cancelClick() {
        cancelHandle?()
    }
}
